package com.hj.ct.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.hj.ct.R;
import com.hj.ct.activity.TableActivity;
import com.hj.ct.logic.LogicActions;
import com.hj.ct.logic.LoginLogic;
import com.hj.ct.model.SetAlarm;
import com.hj.ct.parse.GatherData;
import com.hj.ct.util.DateUtil;
import com.hj.ct.util.SetAlarmUtil;

public class TableFragment extends BaseFragment {

	private ScrollView scrollView;
	private TableLayout tableLayout;
	private View rootView;
	private View emptyView;

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_table, null);
		initView();
		return rootView;
	}

	public View findViewById(int id) {
		return rootView.findViewById(id);
	}

	private void initView() {
		scrollView = (ScrollView) findViewById(R.id.scrollView);
		tableLayout = (TableLayout) findViewById(R.id.table);
		emptyView = findViewById(R.id.emptyview);
	}

	private void initTable(List<GatherData> datas) {
		scrollView.scrollTo(0, 0);
		tableLayout.removeAllViews();
		if (datas.isEmpty()) {
			emptyView.setVisibility(View.VISIBLE);
			return;
		} else {
			emptyView.setVisibility(View.GONE);
		}
		initBaseColum();
		Map<String, SetAlarm> alarmMaps = LoginLogic.getInstance().getSetAlarm();
		int dur = ((TableActivity) getActivity()).getDur();
		for (int i = 0; i < datas.size(); i++) {
			TableRow row = new TableRow(getActivity());
			row.setBackgroundResource(itemColors[i % 2]);
			row.setPadding(10, 15, 10, 15);
			TextView tvTime = new TextView(getActivity());
			TextView tvDust = new TextView(getActivity());
			TextView tvNoise = new TextView(getActivity());
			TextView tvWindSpeed = new TextView(getActivity());
			TextView tvWindDirection = new TextView(getActivity());

			// 时间 粉尘浓度 噪声值 风速 风向
			GatherData gatherData = datas.get(i);
			tvTime.setText(DateUtil.formatResponseTime(gatherData.getRecTime(), dur));
			String dust = gatherData == null ? "-" : gatherData.getDustInst();
			String noise = gatherData == null ? "-" : gatherData.getLAFMaxPrf();
			String windSpeed = gatherData == null ? "-" : gatherData.getWindSP();
			String windDirection = gatherData == null ? "-" : gatherData.getWindDir();
			tvDust.setText("-".equals(dust) ? dust : dust + "mg/m³");// 粉尘
			tvNoise.setText("-".equals(noise) ? noise : noise + "db");// 噪声
			tvWindSpeed.setText(windSpeed);// 风速
			tvWindDirection.setText(windDirection);// 风向

			float warnDust = -1f;
			float warnNoise = -1f;
			SetAlarm alarm = alarmMaps.get(gatherData.getDeviceID());
			if (alarm != null) {
				warnDust = SetAlarmUtil.getWarnDust(alarm);
			}
			if (alarm != null) {
				warnNoise = SetAlarmUtil.getWarnNoise(alarm);
			}

			if (!"-".equals(dust) && warnDust > -1 && Float.valueOf(dust) >= warnDust) {
				tvDust.setTextColor(getResources().getColor(R.color.g_red));
			} else {
				tvDust.setTextColor(getResources().getColor(R.color.g_green));
			}
			if (!"-".equals(noise) && warnNoise > -1 && Float.valueOf(noise) >= warnNoise) {
				tvNoise.setTextColor(getResources().getColor(R.color.g_red));
			} else {
				tvNoise.setTextColor(getResources().getColor(R.color.g_green));
			}

			row.addView(tvTime);
			row.addView(tvDust);
			row.addView(tvNoise);
			row.addView(tvWindSpeed);
			row.addView(tvWindDirection);
			tableLayout.addView(row);
		}
	}

	private void initBaseColum() {
		TableRow baseRow = new TableRow(getActivity());
		baseRow.setBackgroundResource(R.color.gray_cccccc);
		baseRow.setPadding(10, 15, 10, 10);
		TextView tvTime = new TextView(getActivity());
		TextView tvDust = new TextView(getActivity());
		TextView tvNoise = new TextView(getActivity());
		TextView tvWindSpeed = new TextView(getActivity());
		TextView tvWindDirection = new TextView(getActivity());

		// 时间 粉尘浓度 噪声值 风速 风向
		tvTime.setText("时间");
		tvDust.setText("粉尘浓度");
		tvNoise.setText("噪声值");
		tvWindSpeed.setText("风速");
		tvWindDirection.setText("风向");

		baseRow.addView(tvTime);
		baseRow.addView(tvDust);
		baseRow.addView(tvNoise);
		baseRow.addView(tvWindSpeed);
		baseRow.addView(tvWindDirection);
		tableLayout.addView(baseRow);
	}

	public static final int[] itemColors = { R.color.gray_eeeeee, R.color.white };

	public void handlerUI(int action, int code, Object... obj) {
		if (action == LogicActions.HISTORY_DATA_SUCCESS) {
			List<GatherData> datas = (List<GatherData>) obj[0];
			initTable(datas);
		} else if (action == LogicActions.NEED_TO_LOGIN) {
			showReLoginDialog();
		} else if (action == LogicActions.HISTORY_DATA_ERROR) {
			toast((String) obj[0]);
			initTable(new ArrayList<GatherData>());
		}
	}

}
